program main_onehalf
	
	use QMC
	
	implicit NONE
	
	integer(4), parameter		:: nmax = 262144		!131072

	integer(4)					:: iunit, sts
	real(8)						:: tt
	real(8)						:: beta
	
	integer(4)					:: H(L)
	
	integer(4)					:: nmc
	integer(4)					:: i, nvr
	
	real(8)						:: y, z
	
	real(8)						:: s, s2
	real(8)						:: store(0:nmax)
	real(8)						:: store2(0:nmax)
	real(8)						:: avrrr(0:nmax)
	
	integer(4)					:: ip, np, nc
	real(8)						:: mmin, mmax
	
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
	
	tt = 1.0d0
	beta = 1.0d0 / tt
	
	call Init()
	
	open( newunit = iunit, file='reshalf.txt', status = 'replace', action = 'write')
	
	H = 0
	sites = 1
	s = 0.0d0
	s2 = 0.0d0
	
	store(0) = 0.0d0
	store2(0) = 0.0d0
	
	do nmc = 1, nmax
		call RANDOM_NUMBER(y)
		
		if ( y > 0.5) then 
			call GlobalStep (H)
		else
			call RANDOM_NUMBER(z)
			if ( z > 0.5) then
				call LocalDuoStep(beta,H)
			else
				call LocalUniStep(beta,H)
			end if
		end if
		
		nc = count( H /= 0)
		
		store(nmc) = store(nmc-1) + nc
		store2(nmc) = store2(nmc-1) + nc**2
		
		if ( mod(nmc,2) == 0) then
			continue
			
			np = nmc / 2
			forall( ip = np+1:nmc) avrrr(ip) = store(ip) / ip
			
			mmin = minval( avrrr(np+1:nmc))
			mmax = maxval( avrrr(np+1:nmc))
			
			continue
			write( iunit, *) nmc, -mmin, -mmax
			
		end if
		
			
	end do
	
	close( iunit)
end program main_onehalf 